<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head th:include="admin/head :: head" th:with="title=公告编辑">
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" action="" lay-filter="formfilter">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>标题:</label>
                    <div class="layui-input-block">
                            <input type="text" id="title" name="title" lay-verify="required|title" autocomplete="off"  class="layui-input" th:value="${ m.title}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>内容:</label>
                    <div class="layui-input-block">
                        <textarea name="content" lay-verify="content" id="contentEditor" th:text="${m.content}"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>状态:</label>
                    <div class="layui-input-block">
                            <input type="checkbox" th:checked="${ m.status==1}" lay-filter="statusShow" lay-skin="switch" lay-text="显示|不显示">
                            <input type="hidden" id="status" name="status" th:value="${m.status}">
                    </div>
                </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="hidden" name="id" th:value="${ m.id}">
                    <button class="layui-btn" lay-submit="" lay-filter="formSubmit">立即提交</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    layui.use(['layedit','layer', 'jquery','liuQi'], function () {
            var form = layui.form,
                liuQi = layui.liuQi,
                layedit = layui.layedit;

            //日期
            form.on('switch(statusShow)', function(data){
                $("#status").val(this.checked?1:0);
            });
            layedit.set({
                //暴露layupload参数设置接口 --详细查看layupload参数说明
                uploadImage: {
                    url: '/search/fileUpload',
                    accept: 'image',
                    acceptMime: 'image/*',
                    exts: 'jpg|png|gif|bmp|jpeg',
                    size: 1024 * 10,
                }
                , uploadVideo: {
                    url: '/search/fileUpload',
                    accept: 'video',
                    acceptMime: 'video/*',
                    exts: 'mp4|flv|avi|rm|rmvb',
                    size: 1024 * 10 * 2,
                }
                , uploadFiles: {
                    url: '/search/fileUpload',
                    accept: 'file',
                    acceptMime: 'file/*',
                    size: '20480',
                }
                //右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
                //传递参数：
                //图片： imgpath --图片路径
                //视频： filepath --视频路径 imgpath --封面路径
                , calldel: {
                    url: '/search/fileUpload',
                }
                //开发者模式 --默认为false
                , devmode: false
                //插入代码设置 --hide:true 等同于不配置codeConfig
                , codeConfig: {
                    hide: false,  //是否显示编码语言选择框
                    default: 'javascript' //hide为true时的默认语言格式
                }
                //插入自定义链接
                , customlink:{
                    title: '插入layui官网'
                    , href: 'https://www.layui.com'
                    ,onmouseup:''
                }
                , videoAttr: ' preload="none" '
                , tool: [
                    'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'fontfamily','fontSize', 'fontBackColor', 'colorpicker', 'face'
                    , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'images', 'image_alt', 'video','attachment', 'anchors'
                    , '|'
                    , 'table','customlink'
                    , 'fullScreen'
                ]
                , height: '600px'
            });
            var index=layedit.build('contentEditor');
            form.render();

            //自定义验证规则
            form.verify({
                //radio验证
                otherReq: function(value,item){
                    var $ = layui.$;
                    var verifyName=$(item).attr('name')
                            , verifyType=$(item).attr('type')
                            ,formElem=$(item).parents('.layui-form')//获取当前所在的form元素，如果存在的话
                            ,verifyElem=formElem.find('input[name='+verifyName+']')//获取需要校验的元素
                            ,isTrue= verifyElem.is(':checked')//是否命中校验
                            ,focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
                    if(!isTrue || !value){
                        //定位焦点
                        focusElem.css(verifyType=='radio'?{"color":"#FF5722"}:{"border-color":"#FF5722"});
                        //对非输入框设置焦点
                        focusElem.first().attr("tabIndex","1").css("outline","0").blur(function() {
                            focusElem.css(verifyType=='radio'?{"color":""}:{"border-color":""});
                        }).focus();
                        return '必填项不能为空';
                    }
                },
                title: function (value) {
                },
                content: function (value) {
                    //不添加  富文本提交无值
                    return layedit.sync(index);
                },
                status: function (value) {
                },
            });
        //监听提交
            form.on('submit(formSubmit)', function (data) {
                data=data.field;
                liuQi.ajax("/admin/content/update",data,
                        function (data) {
                            //关闭
                            parent.layer.msg(data.msg);
                            parent.layui.table.reload('currentTableId');
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }
                );
                return false;
            });
        });
</script>

</body>
</html>